//
//  SearchResultModel.swift
//  Juejin App Demo
//
//  Created by 葛鹏 on 2021/8/22.
//

import Foundation


class SearchResultModel: ObservableObject {
    
    @Published var results: [SearchResult] = []

    
    init() {
        let pathString = Bundle.main.path(forResource: "SearchResult", ofType: "json")
        
        if let path = pathString {
            
            let url = URL(fileURLWithPath: path)
            
            do{
                
                let data = try Data(contentsOf: url)
                
                let decoder = JSONDecoder()
                
                DispatchQueue.main.async {
                    do {
                        self.results = try decoder.decode([SearchResult].self, from: data)
                    } catch {
                        print("Decode Json Error")
                    }
                }
            }catch{
                print("Parse Data Error")
            }
            
        }
    }
}
